package com.zx.flow.api.execute.response;

import com.zx.flow.api.execute.request.BaseForm;
import com.zx.flow.api.util.Util;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Data
@Schema(description = "响应返回结果")
public class Response<T> {


    @Schema(description = "响应码")
    private String code;

    @Schema(description = "响应信息", example = "成功或失败")
    private String msg;

    @Schema(description = "响应数据")
    private T data;

    public static <T> Response<T> build(ResponseStatus status, String msg, T data) {
        Response<T> response = new Response<>();
        response.setCode(status.getCode());
        response.setMsg(msg);
        response.setData(data);
        return response;
    }

    public static <T> Response<T> success() {
        return build(ResponseStatus.OK, "", null);
    }

    public static <T> Response<T> success(T data) {
        return build(ResponseStatus.OK, "", data);
    }

    public static <T> Response<T> fail(String errorMsg) {
        return build(ResponseStatus.FLOW_ERROR_702, errorMsg, null);
    }

    public static <T> Response<T> fail(ResponseStatus status, String errorMsg) {
        return build(status, errorMsg, null);
    }

    public static <T> Response<T> fail(ResponseStatus status) {
        return build(status, status.getMsg(), null);
    }

    /**
     * 用于校验参数的异常处理
     *
     * @param form
     */
    public static <T> Response<T> fail(BaseForm form) {
        return build(ResponseStatus.FLOW_ERROR_701, form.getErrorMsg(), null);
    }

    @Override
    public String toString() {
        return Util.getJsonStr(this);
    }
}
